package jerry.cn.rfid_module.Setting.ReadList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.zebra.rfid.api3.InvalidUsageException;
import com.zebra.rfid.api3.ReaderDevice;

import java.util.ArrayList;
import java.util.List;

import jerry.cn.rfid_module.R;
import jerry.cn.rfid_module.Setting.Adapter.MainListAdapter;
import jerry.cn.rfid_module.Setting.Fragment.MainListFragment;
import jerry.cn.rfid_module.Setting.StaticClass;
import zuo.biao.library.base.BaseListFragment;
import zuo.biao.library.interfaces.AdapterCallBack;
import zuo.biao.library.model.Entry;

public class ReadListFragment extends BaseListFragment<Entry<String, String>, ListView, MainListAdapter> {

    public static ArrayList<ReaderDevice> readersList = new ArrayList<>();

    public static MainListFragment createInstance() {
        return new MainListFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.fragment_mainlist);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
        readersList.clear();
        loadPairedDevices();
//        if (StaticClass.mConnectedDevice != null) {
//            int index = readersList.indexOf(Application.mConnectedDevice);
//            if (index != -1) {
//                readersList.remove(index);
//                readersList.add(index, Application.mConnectedDevice);
//            } else {
//                Application.mConnectedDevice = null;
//                Application.mConnectedReader = null;
//            }
//        }
        onRefresh();
        return view;//返回值必须为view
    }

    @Override
    public void setList(final List<Entry<String, String>> list) {
        //示例代码<<<<<<<<<<<<<<<
        setList(new AdapterCallBack<MainListAdapter>() {

            @Override
            public void refreshAdapter() {
                adapter.refresh(list);
            }

            @Override
            public MainListAdapter createAdapter() {
                return new MainListAdapter(context);
            }
        });
        //示例代码>>>>>>>>>>>>>>>
    }


    @Override
    public void getListAsync(int page) {
       
    }

    private void loadPairedDevices() {
        try {
            readersList.addAll(StaticClass.readers.GetAvailableRFIDReaderList());
        } catch (InvalidUsageException e) {
            e.printStackTrace();
        }
    }
}
